// DatabaseBase.cpp: implementation of the DatabaseBase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DatabaseBase.h"
#include "UEConnectionManager.h"
#include "UEException.h"
#include "DatabaseInfo.h"
#include "ObjectRegister.h"
#include "SiteTable.h"
#include "ObjectPaternityMap.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace PDMS
{
	namespace PDMSDBIF
	{
		DatabaseBase::DatabaseBase( DatabaseElementRegister* pRegister )
		{
			ASSERT( pRegister );
			m_pRegister = pRegister;
			m_pObjReg = NULL;
			m_pSiteTable = NULL;
			m_pDBInfo = NULL;
			m_pObjPat = NULL;
		}
		
		DatabaseBase::DatabaseBase()
		{
			
		}

		
		DatabaseBase::~DatabaseBase()
		{
			
		}

		UEConnectionPtr DatabaseBase::DataSource() const
		{
			UEConnectionPtr ptr;
			ASSERT( !m_strSource.IsEmpty() );

			try
			{
				ptr = UEConnectionManager::Instance().GetConnection( m_strSource );
			}
			catch( UEException* pE )
			{
				pE->ReportError();
				pE->Delete();
			}
			return ptr;
		}
		
		ObjectRegister* DatabaseBase::GetObjectRegister()
		{
			if ( m_pObjReg == NULL )
			{
				DatabaseElement* pDE = GetDatabaseElement( _T("ObjRegister") );
				if ( pDE->IsKindOf( UE_RUNTIME_CLASS( ObjectRegister ) ) )
				{
					m_pObjReg = static_cast<ObjectRegister*>(pDE);
				}
			}
			return m_pObjReg;
		}

		SiteTable* DatabaseBase::GetSiteTable()
		{
			if ( m_pSiteTable == NULL )
			{
				DatabaseElement* pDE = GetDatabaseElement( _T("SiteData") );
				if ( pDE->IsKindOf( UE_RUNTIME_CLASS( SiteTable ) ) )
				{
					m_pSiteTable = static_cast<SiteTable*>(pDE);
				}
			}
			return m_pSiteTable;
		}

		//ObjectPaternityMap* GetObjectPaternityMap();
		DatabaseInfo* DatabaseBase::GetDatabaseInfo()
		{
			if ( m_pDBInfo == NULL )
			{
				DatabaseElement* pDE = GetDatabaseElement( _T("DBInfo") );
				if ( pDE->IsKindOf( UE_RUNTIME_CLASS( DatabaseInfo ) ) )
				{
					m_pDBInfo = static_cast<DatabaseInfo*>(pDE);
				}
			}
			return m_pDBInfo;
		}
		
		ObjectPaternityMap* DatabaseBase::GetObjectPaternityMap()
		{
			if ( m_pObjPat == NULL )
			{
				DatabaseElement* pDE = GetDatabaseElement( _T("PaternityMap") );
				if ( pDE->IsKindOf( UE_RUNTIME_CLASS( ObjectPaternityMap ) ) )
				{
					m_pObjPat = static_cast<ObjectPaternityMap*>(pDE);
				}
			}
			return m_pObjPat;
		}

		UESoft::ErrorStatus DatabaseBase::UpdateAll()
		{
			UESoft::ErrorStatus es = UESoft::eOk;
			DatabaseElementMap::IteratorPtr it = m_map.NewIterator();
			for ( it->Begin(); !it->End(); it->Next() )
			{
				es = it->Get()->Update();				
			}
			return es;
		}
		
		UESoft::ErrorStatus DatabaseBase::CloseAll()
		{
			UESoft::ErrorStatus es = UESoft::eOk;
			DatabaseElementMap::IteratorPtr it = m_map.NewIterator();
			for ( it->Begin(); !it->End(); it->Next() )
			{
				es = it->Get()->Close();				
			}
			return es;
		}

		DatabaseElement* DatabaseBase::GetDatabaseElement( const StdString& strElementID )
		{
			DatabaseElement* pReg = NULL;
			pReg = m_map.GetManaged( strElementID );
			if ( pReg == NULL )
			{
				pReg = m_pRegister->CreateObject( strElementID );
				ASSERT( pReg );
				pReg->SetDatabase( this );
				m_map.Add( strElementID, pReg );
			}

			return pReg;
		}
	}
}
